package org.zsl.dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.zsl.model.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MybatisTestor {


    @Test
    public void testSelectInMybatis() throws IOException {

        InputStream inputStream = Resources.getResourceAsStream("mybatis.cf.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        String statement = "org.zsl.dao.mybatis.UserMapper.selectAll";

        List<User> userList= sqlSession.selectList(statement);

        for (User user :userList){
            System.out.println(user);
        }
    }

    @Test
    public  void  testInserInMybatis() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis.cf.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        User user = new User();
        user.setUsername("zsl001");
        user.setPassword("123");
        user.setRoleId(10);

        String statement = "org.zsl.dao.mybatis.UserMapper.insert";

       int affected=  sqlSession.insert(statement,user);

       sqlSession.commit();

        System.out.println(affected);
    }
}
